package com.sunday.shoufen.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.sunday.common.base.BaseActivity;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.http.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 刘涛 on 2017/10/25.
 */

public class MyFansActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.slide_tab)
    SlidingTabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.right_btn1)
    ImageView rightBtn1;

    private long memberId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_fans);
        ButterKnife.bind(this);
        initView();
        initFragment();
    }


    private void initView() {
        titleView.setText("我的会员");
        rightBtn.setImageResource(R.mipmap.add);
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn1.setImageResource(R.mipmap.share);
        rightBtn1.setVisibility(View.VISIBLE);
        memberId = BaseApp.getInstance().getMember().getId();
    }


    private Fragment[] fragments;
    private List<String> titles = new ArrayList<>();

    private void initFragment() {
        titles.add("钻石会员");
        titles.add("黄金会员");


        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(titles.size());
    }

    @SuppressLint("DefaultLocale")
    public void updateNumber(int page, int num) {
        tabLayout.getTitleView(page).setText(page == 0 ? String.format("钻石会员（%d）", num) :
                String.format("黄金会员（%d）", num));
    }

    public class PagerAdapter extends FragmentPagerAdapter {


        public PagerAdapter(FragmentManager fm) {
            super(fm);
            fragments = new Fragment[titles.size()];
        }

        @Override
        public Fragment getItem(int position) {
            if (fragments[position] == null) {
                fragments[position] = FansListFragment.newInstance(position + 1);
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

    @OnClick({R.id.right_btn, R.id.right_btn1})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_btn:
                intent = new Intent(mContext, SearchMemberActivity.class);
                intent.putExtra("type", 2);
                mContext.startActivity(intent);
                break;
            case R.id.right_btn1:
                String shareUrl = ApiClient.SHARE_URL + memberId;
                ShareWindow shareWindow = new ShareWindow(mContext, shareUrl, mContext.getResources().getString(R.string.share_product_desc),
                        null, "快点一起来加入", null);
                shareWindow.showPopupWindow(rightBtn);
                break;
        }

    }


}
